import createStore, { IPreact } from 'ipreact-for-react'

export interface IStore {
    /** Router */
    pathname?: string;
}

const { connect, dispatch, getState }: IPreact<IStore> = createStore([])({
    pathname: location.search.substring(1)
})

export { connect, dispatch, getState }

export const locationTo = function (pathname: string, title?: string, data?: any) {
    dispatch(state => {
        return {
            ...state,
            pathname,
        }
    })
    history.pushState(data, title, '?' + pathname)
}

addEventListener('popstate', function (e) {
    dispatch(state => {
        return {
            ...state,
            pathname: location.search.substring(1),
        }
    })
})